<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Requests\MessageRequest;
use App\Http\Resources\MessageResource;
use App\Models\Message;
use Illuminate\Http\Request;

class MessageController extends Controller
{
    public function index(Request $request)
    {
        $message = Message::query()->where('user_id', $request->user()->id)
            ->latest()
            ->paginate($request->get('limit', 10));
        return successReturn([
            'list' => MessageResource::collection($message),
            'total' => $message->total(),
            'last_page' => $message->lastPage()
        ]);
    }


    //读取消息
    public function edit(MessageRequest $request)
    {
        $message = Message::query()->where('id', $request->get('id'))
            ->where('user_id', $request->user()->id)
            ->where('read', false)
            ->first();
        if ($message) {
            $message->read = true;
            $message->save();
        }
        return successReturn();
    }
}
